//
// Copyright (c) Microsoft Corporation.  All rights reserved.
//
//
// Use of this sample source code is subject to the terms of the Microsoft
// license agreement under which you licensed this sample source code. If
// you did not accept the terms of the license agreement, you are not
// authorized to use this sample source code. For the terms of the license,
// please see the license agreement between you and Microsoft or, if applicable,
// see the LICENSE.RTF on your install media or the root of your tools installation.
// THE SAMPLE SOURCE CODE IS PROVIDED "AS IS", WITH NO WARRANTIES.
//
/*++
THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
PARTICULAR PURPOSE.

Module Name: taskman.cpp

Abstract:

Notes:
--*/

      
#include <windows.h>
#include "taskman.h"
#include "wbttskmn.h"

#ifdef DEBUG
#include <debug.h>
#endif


#ifdef DEBUG
  DBGPARAM dpCurSettings = {
    TEXT("TASKMAN"), {
    TEXT("ERROR"),TEXT("WARNING"),TEXT("INFO"),TEXT("TRACE"),
    TEXT("TRACEV"),TEXT("NOTIFY"),TEXT("EVENT"),TEXT("MSG"),
    TEXT(""),TEXT(""),TEXT(""),TEXT(""),
    TEXT(""),TEXT(""),TEXT(""),TEXT("") },
    0x0000001F
  };
#endif

const LPWSTR szRegKeyRegisteredClients  = TEXT("Software\\Microsoft\\WBT\\Clients\\Registration");
const LPWSTR szRegKeyConnections        = TEXT("Connections");
const LPWSTR szRegKeyTaskmanSettings    = TEXT("Software\\Microsoft\\WBT\\Shell");
const LPWSTR szRegKeyTaskmanRunAtStartup= TEXT("Software\\Microsoft\\RunAtStartUp");
const LPWSTR szRegValLastPID            = TEXT("LkPid");

extern DLGHDR g_dlghdr;

/*++

--*/

INT WINAPI
WinMain (
   HINSTANCE hInstance,
   HINSTANCE hPrevInstance,
   LPWSTR szCmdLine,
   INT iCmdShow
   ) {
       
   MSG msg;
   PWBTTASKMAN ptm;
   /*
   HWND hwndTaskman = NULL;
   HWND hwndTab1 = NULL;
   HWND hwndTab2 = NULL;   
   */

   ZEROVAR(msg);
   
   if (!FindWindow(szDesktopWndClassName, NULL)) {
      DEBUGMSG(ZONE_ERROR,(TEXT("TASKMAN: Desktop window not running!\r\n")));
      return -1;
      }
              
   if (FindWindow(szTaskmanWndClassName, NULL)) {
      DEBUGMSG(ZONE_ERROR,(TEXT("TASKMAN: Taskman window is already running!\r\n")));
      return -1;
      }

   ptm = new WBTTASKMAN;
   if (!ptm) {
      return -1;
      }
   
   if (!ptm->InitInstance(hInstance, _wtoi(szCmdLine))) {
      delete ptm;
      return -1;
      }
      
   SendMessage(ptm->hwnd, TASKMAN_SHOW, 0, 0);
   ptm->ProcessAutostart();
   
   while (GetMessage(&msg, NULL, 0, 0)) {
	            
      if (( msg.message == WM_KEYDOWN )&&( msg.wParam == VK_TAB )&& GetKeyState(VK_CONTROL) < 0) {
         SendMessage(FindWindow(szTaskmanWndClassName, NULL), TASKMAN_CHANGE_TAB, 0, 0);
         continue;
         }

      if ((msg.message == WM_KEYDOWN)&&(msg.wParam == VK_F2)) {
         SendMessage(FindWindow(szDesktopWndClassName, NULL), WM_KEYDOWN, VK_F2, 0);
         continue;
         }

      if ((ptm->hwnd == NULL || !IsDialogMessage(ptm->hwnd, &msg)) &&
          (g_dlghdr.hwndTab[TASKMAN_TAB1] == NULL || !IsDialogMessage(g_dlghdr.hwndTab[TASKMAN_TAB1], &msg)) &&
          (g_dlghdr.hwndTab[TASKMAN_TAB2] == NULL || !IsDialogMessage(g_dlghdr.hwndTab[TASKMAN_TAB2], &msg))) {
            TranslateMessage(&msg);
            DispatchMessage (&msg);
         }
      
      }   

   while (GetMessage(&msg, NULL, 0, 0)) {               
         TranslateMessage(&msg);
         DispatchMessage(&msg);
         }
      
   delete ptm;
   return msg.wParam;
}


